﻿using System;
using System.Collections.Generic;
using System.Text;
using Vbyte.SharpOrm.SqlDb;

namespace Vbyte.SharpOrm.Adapter
{
    /// <summary>
    /// 相关数据库的SQL语句创建适配
    /// </summary>
    public interface ISqlBuilderAdapter
    {
        /// <summary>
        /// 获取常用SQL语句的格式化定义
        /// </summary>
        /// <param name="cmdType">SQL数据表中单行命令类型</param>
        /// <returns></returns>
        /// <remarks>
        /// {Table} {FieldsList} {ValueList} {TableLINK} {Condition}
        /// {GroupBy} {OrderBy} {ValueSetList}
        /// </remarks>
        string GetSqlFormatText(SQLRowCommandType cmdType);

        /// <summary>
        /// 目标SQL构建格式化语句中的关键词集合
        /// </summary>
        /// <returns>关键词集合</returns>
        string[] GetFormatKeys();

        /// <summary>
        /// 构建命令适配的SQL文本
        /// </summary>
        /// <param name="entry">ORM操作实例原型</param>
        /// <param name="cmdType">SQL数据表中单行命令类型</param>
        /// <param name="isSingleRow">是否时单行操作，如TOP 1等。</param>
        /// <returns>目标SQL语句</returns>
        string BuildCommandText(TableEntry entry, SQLRowCommandType cmdType, bool isSingleRow);

        #region 中间构建常数值
        /// <summary>
        /// 获取或设置SQL语句管理的数据表名（或视图、联合查询SQL表）
        /// </summary>
        string VirtualTable { get; set; }

        /// <summary>
        /// 获取或设置顺序字段名称集合
        /// </summary>
        string[] FieldNamesList { get; set; }

        /// <summary>
        /// 获取或设置顺序字段的值集合(参数占位名或实际值)
        /// </summary>
        string[] FieldValuesList { get; set; }

        /// <summary>
        /// 获取或设置制定字段的值集合(SQL Update)
        /// </summary>
        string[] CustomValueSetList { get; set; }

        /// <summary>
        /// 条件约束(where)
        /// </summary>
        string SqlCondition { get; set; }

        /// <summary>
        /// 分组约束(Group by)
        /// </summary>
        string SqlGroupBy { get; set; }

        /// <summary>
        /// 排序约束(Order By)
        /// </summary>
        string SqlOrderBy { get; set; }

        /// <summary>
        /// 数据获取限制(TOP)
        /// </summary>
        string SqlLimit { get; set; }
        #endregion

        /// <summary>
        /// 设置返回记录的最大条数
        /// </summary>
        /// <param name="imaxRows">最大条数</param>
        void SetRecordLimit(int imaxRows);


        /// <summary>
        /// 是否实现了约束的表达式语句构建
        /// </summary>
        bool HasImplementConstraintExp();

        /// <summary>
        /// 构建SQL约束表达式
        /// </summary>
        /// <param name="build">实现了约束表达式构建的实例</param>
        /// <param name="dbOwner">数据库所有者占位前缀</param>
        /// <param name="tableName">数据库表名</param>
        /// <param name="columnName">涉及到的列名</param>
        /// <returns>最终的约束表达式语句</returns>
        string BuildConstraintExpression(ISqlConstraintExpBuild build, string dbOwner, string tableName, string columnName);

    }

}
